using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using InfraStructureLayer;
using System.Collections;
using System.Data;
using EntityLayer;
using BusinessLayer;

namespace WebControls
{
    public class SFHttpHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private int _ReferrerId = 0;

        public int ReferrerId
        {
            get { return _ReferrerId; }
            set { _ReferrerId = value; }
        }
        protected string GenerateUrl(object obj)
        {
            Crypto insCrypto = new Crypto();
            return InfraStructureLayer.CurrentContext.Parameters["WebSiteAddress"].ToString() + "Handlers/Redirect.ashx?Address=" + insCrypto.EncryptURL(obj.ToString()).Replace("&", "&amp;");
        }
        protected string Encrypt(object obj)
        {
            Crypto insCrypto = new Crypto();
            return insCrypto.EncryptURL(obj.ToString()).Replace("&", "&amp;");
        }
        protected string GenerateUrlPhoto(object obj)
        {
            Crypto insCrypto = new Crypto();
            return InfraStructureLayer.CurrentContext.Parameters["WebSiteAddress"].ToString() + "Handlers/Image.ashx?Url=" + insCrypto.EncryptURL(obj.ToString()).Replace("&", "&amp;") + "&Width=550".Replace("&", "&amp;");
        }
        public virtual void ProcessRequest(HttpContext context)
        {

            if (CurrentContext.Querystrings == null)
            {
                busQuerystrings insBusQuerystrings = new busQuerystrings();
                DataTable insDataTableQuerystrings = insBusQuerystrings.SelectQuerystrings();
                Dictionary<string, string> insQuerystrings = new Dictionary<string, string>();
                foreach (DataRow dr in insDataTableQuerystrings.Rows)
                {
                    insQuerystrings.Add(dr["Querystring"].ToString(), dr["Guid"].ToString());
                }
                CurrentContext.Querystrings = insQuerystrings;
            }
            if (CurrentContext.Pages == null)
            {
                BusinessLayer.busPages insBusPages = new BusinessLayer.busPages();
                System.Data.DataTable insDataTablePages = insBusPages.SelectPages();

                CurrentContext.Pages = insDataTablePages;
            }
            if (CurrentContext.Multilanguage == null)
            {
                BusinessLayer.busLanguages insBusLanguages = new BusinessLayer.busLanguages();
                System.Data.DataTable insDataTableLanguages = insBusLanguages.SelectLanguages();

                Hashtable insHashTableMultiLanguage = new Hashtable();
                foreach (System.Data.DataRow dr in insDataTableLanguages.Rows)
                {
                    foreach (DataRow drPages in CurrentContext.Pages.Rows)
                    {
                        entMultilanguageSettings insEntMultilanguageSettings = new entMultilanguageSettings();
                        insEntMultilanguageSettings.LanguageId = Convert.ToInt32(dr["LanguageId"]);
                        insEntMultilanguageSettings.PageId = Convert.ToInt32(drPages["PageId"]);
                        busMultilanguageSettings insBusMultilanguageSettings = new busMultilanguageSettings();
                        System.Data.DataTable insDataTableMultilanguageSettings = insBusMultilanguageSettings.SelectMultilanguageSettingsByLanguageIdAndPageId(insEntMultilanguageSettings);
                        insHashTableMultiLanguage.Add(insEntMultilanguageSettings.ToString(), insDataTableMultilanguageSettings);
                    }
                }
                CurrentContext.Multilanguage = insHashTableMultiLanguage;
            }
            if (CurrentContext.ErrorMessagesMultilanguage == null)
            {
                busErrorMessageMultilanguageSettings insBusErrorMessageMultilanguageSettings = new busErrorMessageMultilanguageSettings();
                DataTable insDataTableErrorMessageMultilanguageSettings = insBusErrorMessageMultilanguageSettings.SelectErrorMessageMultilanguageSettings();

                Hashtable insHashTableErrorMessagesMultilanguage = new Hashtable();
                foreach (System.Data.DataRow dr in insDataTableErrorMessageMultilanguageSettings.Rows)
                {
                    insHashTableErrorMessagesMultilanguage.Add(dr["LanguageId"].ToString() + "|" + dr["ErrorMessageId"].ToString(), dr["ErrorMessageText"].ToString());
                }
                CurrentContext.ErrorMessagesMultilanguage = insHashTableErrorMessagesMultilanguage;
            }
            if (CurrentContext.Menu == null)
            {
                BusinessLayer.busMenus insBusMenus = new BusinessLayer.busMenus();
                System.Data.DataTable insDataTableMenus = insBusMenus.SelectMenus();

                CurrentContext.Menu = insDataTableMenus;
            }
            if (CurrentContext.Languages == null)
            {
                BusinessLayer.busLanguages insBusLanguages = new BusinessLayer.busLanguages();
                System.Data.DataTable insDataTableLanguages = insBusLanguages.SelectLanguages();

                CurrentContext.Languages = insDataTableLanguages;
            }
            if (CurrentContext.Parameters == null)
            {
                busParameters insBusParameters = new busParameters();
                DataTable insDataTableParameters = insBusParameters.SelectParameters();
                Hashtable insHashtableParameters = new Hashtable();
                foreach (DataRow dr in insDataTableParameters.Rows)
                {
                    insHashtableParameters.Add(dr["ParameterName"].ToString(), dr["ParameterValue"].ToString());
                }
                CurrentContext.Parameters = insHashtableParameters;
            }
            if (System.Web.HttpContext.Current.Request.QueryString["LanguageId"] != null && System.Web.HttpContext.Current.Request.QueryString["LanguageId"] != "")
            {
                if (CurrentContext.Languages.Select("LanguageId=" + System.Web.HttpContext.Current.Request.QueryString["LanguageId"]).Length == 0)
                {
                    context.Response.Redirect("~/default.aspx;");
                }
                CurrentContext.LanguageId = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["LanguageId"]);
            }
            if (System.Web.HttpContext.Current.Items["LanguageId"] != null)
            {
                CurrentContext.LanguageId = Convert.ToInt32(System.Web.HttpContext.Current.Items["LanguageId"]);
                System.Web.HttpContext.Current.Items["LanguageId"] = null;
                //Response.Redirect(Request.Url.PathAndQuery);

            }
            if (CurrentContext.LanguageId == null)
            {
                if (System.Web.HttpContext.Current.Request.Cookies["Language"] != null)
                {
                    
                    CurrentContext.LanguageId = Convert.ToInt32(System.Web.HttpContext.Current.Request.Cookies["Language"]["LanguageId"]);
                }
                else
                {
                    if (context.Request.Headers != null && context.Request.Headers["Accept-Language"] != null && context.Request.Headers["Accept-Language"].Length>=2)
                    {
                        System.Data.DataRow[] drArr = InfraStructureLayer.CurrentContext.Languages.Select("LanguageCulture like '%" + context.Request.Headers["Accept-Language"].Substring(0, 2) + "%'");
                        if (drArr.Length > 0)
                        {
                            InfraStructureLayer.CurrentContext.LanguageId = Convert.ToInt32(drArr[0]["LanguageId"]);
                        }
                        else
                        {
                            InfraStructureLayer.CurrentContext.LanguageId = Convert.ToInt32(InfraStructureLayer.CurrentContext.Parameters["DefaultLanguage"]);
                        }
                    }
                    else
                    {
                        InfraStructureLayer.CurrentContext.LanguageId = Convert.ToInt32(InfraStructureLayer.CurrentContext.Parameters["DefaultLanguage"]);
                    }
                }
            }


            busReferrers insBusReferrers = new busReferrers();
            entReferrers insEntReferrers = new entReferrers();
            insEntReferrers.Browser = context.Request.Browser.Browser;
            insEntReferrers.IpAddress = context.Request.UserHostAddress;
            insEntReferrers.IsRssReferrer = true;
            insEntReferrers.LanguageId = CurrentContext.LanguageId;
            insEntReferrers.PageId = null;
            if (context.Request.UrlReferrer != null)
            {
                insEntReferrers.ReferrerAddress = context.Request.UrlReferrer.PathAndQuery.ToString();
            }
            insEntReferrers.ReferrerDate = DateTime.Now;
            insBusReferrers.InsertReferrers(insEntReferrers);
            ReferrerId = insEntReferrers.ReferrerId;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

            
    }
}
